﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseUi
{
    /// <summary>
    /// 用户
    /// </summary>
    [SugarTable("User")]
    public class UserBase
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }
        /// <summary>
        /// 登录名
        /// </summary>
        public string LoginName { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }
        /// <summary>
        /// 角色id（json）
        /// </summary>
        [SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString, IsJson = true)]
        public List<int> RoleIds { get; set; } = new List<int>();
        /// <summary>
        /// 角色名称
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public List<string> RoleNames { get => (RoleIds == null || !RoleIds.Any()) ? new List<string>() : AuthDb.db.Queryable<RoleBase>().Where(o => RoleIds.Contains(o.Id)).Select(o => o.Name).ToList(); }
        /// <summary>
        /// 是否最大权限
        /// </summary>
        public bool IsAdmin { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime Time { get; set; }

        /// <summary>
        /// 用户所属角色
        /// </summary>
        public List<RoleBase> GetRoles(List<RoleBase> roleAll = null)
        {
            if (RoleIds == null || !RoleIds.Any())
                return new List<RoleBase>();

            if (roleAll == null)
                return AuthDb.db.Queryable<RoleBase>().Where(o => RoleIds.Contains(o.Id)).OrderBy(o => o.Id).ToList();
            else
                return roleAll.Where(o => RoleIds.Contains(o.Id)).OrderBy(o => o.Id).ToList();
        }
    }

    /// <summary>
    /// 角色
    /// </summary>
    [SugarTable("Role")]
    public class RoleBase
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }
        /// <summary>
        /// 角色名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 认证模块（json）
        /// </summary>
        [SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString, IsJson = true)]
        public List<int> Auths { get; set; } = new List<int>();
        /// <summary>
        /// 认证模块名称
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public List<string> AuthNames { get => (Auths == null || !Auths.Any()) ? new List<string>() : EnumHelps.GetEnumDescriptionList(typeof(AuthEnum), true).Where(o => Auths.Contains(o.Item1)).Select(o => o.Item3).ToList(); }
        /// <summary>
        /// 是否最大权限
        /// </summary>
        public bool IsAdmin { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime Time { get; set; }
    }
    [SugarTable("BoxInfo")]
    public class BoxInfo
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }
        /// <summary>
        /// 测试位
        /// </summary>
        public string Num { get; set; }
        /// <summary>
        /// 状态（ok11,ng(1.2.3.....)）
        /// </summary>
        public string State { get; set; }
        /// <summary>
        /// 是否合格
        /// </summary>
        public string IsOk { get; set; }
        /// <summary>
        /// 测试人员
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 人工确认
        /// </summary>
        public string Artificial { get; set; }
        /// <summary>
        /// 机型
        /// </summary>
        public string Type { get; set; }
        /// <summary>
        /// 时间
        /// </summary>
        public DateTime Time { get; set; }

    }
}
